/* Author: David McCoy dave.mccoy@cospandesign.com
 *
 *     This file is part of DLCM\SubServ.
 *
 *  DLCM\SubServ is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DLCM\SubServ is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DLCM\SubServ.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * StructureViewer.java
 *
 * Used to view uninitialize structures of LCM
 *
 * Created on Mar 26, 2010, 10:08:32 PM
 */

package com.cospandesign.gui.structures;

import dlcm.Member;
import dlcm.Structure;
import dlcm.StructureManager;
import java.lang.Object;
import java.util.ArrayList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author cospan
 */
public class StructureViewer extends javax.swing.JFrame {

    StructureManager mStructureManager;

    /** Creates new form StructureViewer */
    public StructureViewer() {
        initComponents();
        
        StructureList.addListSelectionListener(new ListSelectionListener(){

            public void valueChanged(ListSelectionEvent e) {
                listSelectionChanged();
            }

            
        });
        
        
    }

    public void setStructureManager(StructureManager structureManager){
        mStructureManager = structureManager;
    }
    public void updateStructureManager(){
        //upate the list
        ArrayList<String> structureNameArray = new ArrayList<String>();


        DefaultTableModel model = (DefaultTableModel) MemberTable.getModel();
        while (model.getRowCount() > 0){
            model.removeRow(0);
        } 

        for (String name : mStructureManager.StructureMap.keySet()){
            structureNameArray.add(name);
        }

        Object [] nameList = new String[structureNameArray.size()];
        nameList = structureNameArray.toArray();
        StructureList.setListData(nameList);
        if (structureNameArray.size() > 0){
            StructureList.setSelectedIndex(0);
        }

    }
    public void listSelectionChanged(){
        if (StructureList.getModel().getSize() == 0){
            EntityNameLabel.setText("Nothing Selected");
            return;
        }
        String name = (String) StructureList.getSelectedValue();
        if (name == null){
            return;
        }
        this.EntityNameLabel.setText(name);

        if (!mStructureManager.StructureMap.containsKey(name)){
            return;
        }

        Structure structure = mStructureManager.StructureMap.get(name);

        MemberTable.removeAll();

        ArrayList<Object> objectArray = new ArrayList<Object>();

        for (Member member : structure.getMemberList()){
            Object [] objects = new Object[3];
            objects[0] = member.Name;
            objects[1] = member.Type;
            objects[2] = member.isConstant();
            objectArray.add(objects);
        }

        DefaultTableModel model = (DefaultTableModel) MemberTable.getModel();
        while (model.getRowCount() > 0){
            model.removeRow(0);
        }

        for (int i = 0; i < objectArray.size(); i++){
            Object[] object = (Object []) objectArray.get(i);
            model.addRow(object);
        }

        //model.setDataVector(((object[objectArrray.objectList), new Object[] {"Name", "Type", "Constant"});
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        StructureList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        MemberTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        EntityNameLabel = new javax.swing.JLabel();
        RemoveSelectedButton = new javax.swing.JButton();

        setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(getClass().getResource("/com/cospandesign/resources/cospandesign.png")));
        setName("StructureForm"); // NOI18N
        setResizable(false);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        StructureList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        StructureList.setToolTipText("");
        StructureList.setName("StructureList"); // NOI18N
        jScrollPane1.setViewportView(StructureList);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        MemberTable.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        MemberTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Member", "Type", "Constant"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        MemberTable.setName("MemberTable"); // NOI18N
        jScrollPane2.setViewportView(MemberTable);

        jLabel1.setText("Entities");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Entity Name:");
        jLabel2.setName("jLabel2"); // NOI18N

        EntityNameLabel.setText("Nothing Selected");
        EntityNameLabel.setName("EntityNameLabel"); // NOI18N

        RemoveSelectedButton.setText("Remove Selected");
        RemoveSelectedButton.setName("RemoveSelectedButton"); // NOI18N
        RemoveSelectedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveSelectedButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(EntityNameLabel))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(RemoveSelectedButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(EntityNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(RemoveSelectedButton))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void RemoveSelectedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveSelectedButtonActionPerformed
        if (StructureList.getSelectedIndex() == -1){
            return;
        }

        mStructureManager.removeStructure((String)StructureList.getSelectedValue());
        this.updateStructureManager();;
    }//GEN-LAST:event_RemoveSelectedButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel EntityNameLabel;
    private javax.swing.JTable MemberTable;
    private javax.swing.JButton RemoveSelectedButton;
    private javax.swing.JList StructureList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables



}
